<template>
  <el-dialog :title="!dataForm.id ? '评价类型新增' : '评价类型修改'" :close-on-click-modal="false" :visible.sync="visible" append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="评价类型名称" prop="typeName">
        <el-input v-model="dataForm.typeName" placeholder="评价类型名称"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="dataForm.status" :label="1" border>启用</el-radio>
        <el-radio v-model="dataForm.status" :label="0" border>禁用</el-radio>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getWxEvaluateTypeObj,
    addWxEvaluateTypeObj,
    putWxEvaluateTypeObj
  } from '@/api/mp/wxevaluatetype'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          typeName: '',
          status: 1
        },
        dataRule: {
          typeName: [{
            required: true,
            message: '评价类型名称不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWxEvaluateTypeObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putWxEvaluateTypeObj(this.dataForm).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              });
            } else {
              addWxEvaluateTypeObj(this.dataForm).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            }
          }
        })
      }
    }
  }
</script>
